package com.vornitologist;

import java.util.Map;

import com.vaadin.addon.touchkit.gwt.client.TouchKitOfflineApp;
import com.vaadin.addon.touchkit.ui.TouchKitWindow;
import com.vornitologist.widgetset.client.VornitologistOfflineMode;

/**
 * Top level window for Vornitologist. This classess repsonsibility is to
 * configure various mobile web app related enhancements.
 * <p>
 * This class also takes care of handling data that the "offline mode" (See
 * {@link TouchKitOfflineApp}) sends to the server once users gets online.
 * 
 */
@SuppressWarnings("serial")
final class VornitologistWindow extends TouchKitWindow {

    public VornitologistWindow() {
        setCaption("HawaiiTouch");
        /*
         * These configurations modify how the app behaves as "ios webapp".
         */
        addApplicationIcon(VornitologistApplication.get().getURL()
                + "VAADIN/themes/vornitologist/icon.png");
        setStartupImage(VornitologistApplication.get().getURL()
                + "VAADIN/themes/vornitologist/startup.png");
        setWebAppCapable(true);
        setPersistentSessionCookie(true);

 
    }

    @Override
    public void changeVariables(Object source, Map<String, Object> variables) {
        super.changeVariables(source, variables);
        handleOfflineCollectedData(variables);
    }

    /**
     * This method handles simple messages sent by the
     * {@link VornitologistOfflineMode} when application has become online.
     */
    private void handleOfflineCollectedData(Map<String, Object> variables) {
        if (variables.containsKey("offlineObservations")) {
            String[] observations = (String[]) variables
                    .get("offlineObservations");
           
            showNotification(observations.length
                    + " observation(s) persisted to server!");
        }
    }
}